﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ZYO
{
    public class ZYDateTime
    {
        public Persistence.Obj.ZYDateTime Xml;

        public ZYDateTime()
        {
            Xml = new ZYO.Persistence.Obj.ZYDateTime();
        }

        public ZYDateTime(DateTime dtTime):this()
        {
            this.Xml.当前时间 = dtTime;
            ini();
        }

        public ZYDateTime(int year, int month, int day, int hour):this()
        {
            this.Xml.当前时间 = new DateTime(year, month, day, hour, 0, 1);
            ini();
        }

        public ZYDateTime(Persistence.Obj.ZYDateTime per)
            : this()
        {
            this.Xml = per;
        }

        private void ini()
        {
            Runtime节气 thisJieQi = new Runtime节气(this.Xml.当前时间);
            this.Xml.本月的节气 = thisJieQi.Xml;

            int theYear;
            int theMonth;
            int theDay;
            int theHour;

            //计算 年 甲子===============================================================
            theYear = this.Xml.当前时间.Year;
            DateTime dtFeb = new DateTime(theYear, 2, 8);//For 2月份的节气
            Runtime节气 jq = new Runtime节气(dtFeb); //检查2月份的节气，以判断年份

            if (jq.Xml.节气日期 > this.Xml.当前时间)
                theYear = theYear - 1;

            theYear = theYear - 1983;

            while (theYear < 0)
            {
                theYear = theYear + 60;
            }

            this.Xml.年 = new Runtime甲子(theYear).Xml;

            //计算 月 甲子===============================================================

            theMonth = this.Xml.当前时间.Month;

            if (thisJieQi.Xml.节气日期 > this.Xml.当前时间)
            {
                theMonth = theMonth - 1;
            }

            int dz;
            dz = (theMonth - 1 + 2) % 12;

            int yinli; //阴历的月份数

            //将地支 转换为 阴历的月份数======
            if (dz <= 2)  //亥子丑月
                yinli = dz + 10;
            else
                yinli = dz - 2;

            //一月份（寅）的天干为年的天干 *2+1  +月份偏移量 /* - 1（寅）1月*/
            Runtime甲子 class年甲子 = new Runtime甲子(this.Xml.年);

            int tg;

            tg = 2 * (int)class年甲子.Xml.干de枚举值 + 1 + (yinli - 1); //*月份数-偏移量*/

            this.Xml.月 = new Runtime甲子(tg, dz).Xml;

            //计算 日 甲子===============================================================

            DateTime DT1997 = new DateTime(1997, 3, 22, 0, 0, 0, 0);// 1997,3,23是甲子日
            System.TimeSpan Duration = this.Xml.当前时间.Date - DT1997;

            int Dur = Duration.Days;
            while (Dur < 0)
                Dur = Dur + 60000;

            theDay = Dur % 60;

            this.Xml.日 = new Runtime甲子(theDay).Xml;

            //计算 时 甲子===============================================================
            Runtime甲子 class日甲子 = new Runtime甲子(this.Xml.日);

            theHour = this.Xml.当前时间.Hour;

            int dzHour = (theHour + 1) / 2 + 1;
            int tgHour = 2 * (int)class日甲子.Xml.干de枚举值 - 1 + dzHour - 1;

            this.Xml.时 = new Runtime甲子(tgHour, dzHour).Xml;


            // Assign the Name field
            this.Xml.Name = String.Format("{0}年{1}月{2}日{3}时",
                                            this.Xml.年.枚举值.ToString(),
                                            this.Xml.月.枚举值.ToString(),
                                            this.Xml.日.枚举值.ToString(),
                                            this.Xml.时.枚举值.ToString());

            // 计算当前所处的节气
            // 相邻的节气 =========================================

            //class节气 last下旬节气 = this.jqThisMonth--;
            //class节气 this上旬节气 = this.jqThisMonth;
            //class节气 this下旬节气 = this.jqThisMonth++;
            //class节气 next上旬节气 = this下旬节气++;

            Runtime节气 jqTemp;
            for (int i = -1; i <= 2; i++)
            {
                jqTemp = new Runtime节气((int)this.Xml.本月的节气.枚举值 + i, this.Xml.当前时间.Year);
                if (isFutouMatched(class日甲子, jqTemp))
                {
                    this.Xml.所处节气 = jqTemp.Xml;
                    switch (class日甲子.Xml.所属之元de枚举值)
                    {
                        case ZYO.Persistence.Obj.enum元.上元:
                            this.Xml.所处之局 = jqTemp.Xml.上元之局;
                            break;
                        case ZYO.Persistence.Obj.enum元.中元:
                            this.Xml.所处之局 = jqTemp.Xml.中元之局;
                            break;
                        case ZYO.Persistence.Obj.enum元.下元:
                            this.Xml.所处之局 = jqTemp.Xml.下元之局;
                            break;
                    }
                    break;
                }
            }

        }

        private bool isFutouMatched(Runtime甲子 jz, Runtime节气 jq)
        {
            bool isMatched = (jz.Xml.上元符头de枚举值 == jq.Xml.节气上元符头de枚举值);

            if (jq.Xml.is闰)
            {
                if (jz.Xml.上元符头de枚举值 == jq.Xml.闰节气上元符头de枚举值)
                {
                    isMatched = true;
                }
            }

            return isMatched;
        }

    }

    public class Runtime天干 : ZYObject
    {
        public Persistence.Obj.天干 Xml;

        public Runtime天干()
            : base()
        {
            this.Xml = new ZYO.Persistence.Obj.天干();
            this.EnumType = typeof(Persistence.Obj.enum天干);
            this.PersistenceType = typeof(Persistence.Obj.天干);
        }
        public Runtime天干(int id)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum天干>(id);
            AssignProperties();

        }
        public Runtime天干(String strName)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.Str2Enum<ZYO.Persistence.Obj.enum天干>(strName);
            AssignProperties();
        }


        public Runtime天干(Persistence.Obj.enum天干 eTG)
            : this()
        {
            this.Xml.枚举值 = eTG;
            AssignProperties();
        }

        public Runtime天干(Persistence.Obj.天干 persistTianGan)
            : this()
        {
            this.Xml = persistTianGan;
            this.ID = (byte)this.Xml.枚举值;

        }

        public override void AssignProperties()
        {
            this.ID = (byte)this.Xml.枚举值;

            this.Xml.五行属性de枚举值 = (Persistence.Obj.enum五行)DataFactory.ZYDBXML.天干Table[this.ID].五行属性de枚举值;
                
            this.Xml.阴阳 = ZYOShared.Is阴阳((int)this.Xml.枚举值);
            //this.Xml.八字六神 = ZYO.Persistence.Obj.enum八字六神.正财;

        }


        /// <summary>
        /// 以本天干为中心计算参数的角色，如：正官，偏财 等等
        /// </summary>
        /// <param name="TG"> 需算角色的天干</param>
        /// <returns></returns>
        public Persistence.Obj.enum八字六神 Role(Runtime天干 inTG)
        {
            Persistence.Obj.enum八字六神 ret = Persistence.Obj.enum八字六神.偏财; //default value
            if (null == inTG)
            {
                throw new Exception("Null value of input 天干 in Role()");
            }

            switch (ZYOShared.Check五行生克(this.Xml.五行属性de枚举值, inTG.Xml.五行属性de枚举值))
            {
                case Persistence.Obj.e五行生克关系.被生://被生， 印授， 偏印
                    if (this.Xml.阴阳 == inTG.Xml.阴阳)
                    {
                        ret = Persistence.Obj.enum八字六神.偏印;
                    }
                    else
                    {
                        ret = Persistence.Obj.enum八字六神.印授;
                    }
                    break;
                case Persistence.Obj.e五行生克关系.被克://被克， 正官， 煞
                    if (this.Xml.阴阳 == inTG.Xml.阴阳)
                    {
                        ret = Persistence.Obj.enum八字六神.煞;
                    }
                    else
                    {
                        ret = Persistence.Obj.enum八字六神.正官;
                    }
                    break;
                case Persistence.Obj.e五行生克关系.生://生， 食神， 伤官
                    if (this.Xml.阴阳 == inTG.Xml.阴阳)
                    {
                        ret = Persistence.Obj.enum八字六神.食神;
                    }
                    else
                    {
                        ret = Persistence.Obj.enum八字六神.伤官;
                    }
                    break;
                case Persistence.Obj.e五行生克关系.克://克， 正财，偏财
                    if (this.Xml.阴阳 == inTG.Xml.阴阳)
                    {
                        ret = Persistence.Obj.enum八字六神.偏财;
                    }
                    else
                    {
                        ret = Persistence.Obj.enum八字六神.正财;
                    }
                    break;
                case Persistence.Obj.e五行生克关系.比和://相同， 比肩，劫财
                    if (this.Xml.阴阳 == inTG.Xml.阴阳)
                    {
                        ret = Persistence.Obj.enum八字六神.比肩;
                    }
                    else
                    {
                        ret = Persistence.Obj.enum八字六神.劫财;
                    }
                    break;
            }
            return ret;
        }
    }

    public class Runtime地支 : ZYObject
    {
        public Persistence.Obj.地支 Xml;

        public Runtime地支()
            : base()
        {
            this.Xml = new ZYO.Persistence.Obj.地支();
            this.EnumType = typeof(Persistence.Obj.enum地支);
            this.PersistenceType = typeof(Persistence.Obj.地支);
        }

        public Runtime地支(int id)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.ID2Enum<ZYO.Persistence.Obj.enum地支>(id);
            AssignProperties();
        }

        public Runtime地支(String strName)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.Str2Enum<ZYO.Persistence.Obj.enum地支>(strName);
            AssignProperties();

        }

        public Runtime地支(Persistence.Obj.enum地支 eDZ)
            : this()
        {
            this.Xml.枚举值 = eDZ;
            AssignProperties();
        }

        public Runtime地支(Persistence.Obj.地支 persistDizhi)
            : this()
        {
            this.Xml = persistDizhi;
            this.ID = (byte)this.Xml.枚举值;

        }

        public override void AssignProperties()
        {
            this.ID = (byte)this.Xml.枚举值;

            this.Xml.五行属性de枚举值 = (Persistence.Obj.enum五行)(int)DataFactory.ZYDBXML.地支Table[ID].五行属性de枚举值;
            this.Xml.地蔵天干 = DataFactory.ZYDBXML.地支Table[ID].地蔵天干;
            this.Xml.阴阳 = ZYOShared.Is阴阳((int)this.Xml.枚举值);
        }

        public bool is空(Persistence.Obj.ZYDateTime zyDate)
        {
            bool isEmpty = false;
            foreach (Persistence.Obj.地支 dz in zyDate.日.旬空)
            {
                if (dz == this.Xml)
                {
                    isEmpty = true;
                }
            }
            return isEmpty;
        } 
    }

    public class Runtime甲子 : ZYObject
    {
        public Persistence.Obj.甲子 Xml;

        public Runtime甲子()
            : base()
        {
            this.Xml = new ZYO.Persistence.Obj.甲子();
            this.Xml.旬空 = new ZYO.Persistence.Obj.地支[2];

            this.EnumType = typeof(Persistence.Obj.enum甲子);
            this.PersistenceType = typeof(Persistence.Obj.甲子);
        }

        public Runtime甲子(int id)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.ID2Enum<ZYO.Persistence.Obj.enum甲子>(id);
            AssignProperties();
        }

        public Runtime甲子(String strName)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.Str2Enum<ZYO.Persistence.Obj.enum甲子>(strName);
            AssignProperties();
        }

        public Runtime甲子(int 天干ID, int 地支ID)
            : this()
        {
            Runtime天干 tg = new Runtime天干(天干ID);
            Runtime地支 dz = new Runtime地支(地支ID);

            this.Xml.枚举值 = ZYOShared.Str2Enum<ZYO.Persistence.Obj.enum甲子>(tg.Xml.枚举值.ToString() + dz.Xml.枚举值.ToString());

            AssignProperties();
        }
        public Runtime甲子(Persistence.Obj.enum甲子 enumJiaZi)
            : this()
        {
            this.Xml.枚举值 = enumJiaZi;
            AssignProperties();
        }

        public Runtime甲子(Persistence.Obj.甲子 persistJiaZi)
            : this()
        {
            this.Xml = persistJiaZi;
            this.ID = (byte)this.Xml.枚举值;
        }

        public override void AssignProperties()
        {
            this.ID = (byte)this.Xml.枚举值;

            this.Xml.干de枚举值 = ZYOShared.Str2Enum<Persistence.Obj.enum天干>(this.Xml.枚举值.ToString().Substring(0, 1));
            this.Xml.支de枚举值 = ZYOShared.Str2Enum<Persistence.Obj.enum地支>(this.Xml.枚举值.ToString().Substring(1, 1));

            this.Xml.旬首de枚举值 = Rank(10); // 六甲旬首

            this.Xml.旬空[0] = new Runtime地支((int)(this.Xml.旬首de枚举值) - 2).Xml;
            this.Xml.旬空[1] = new Runtime地支((int)(this.Xml.旬首de枚举值) - 1).Xml;

            this.Xml.上元符头de枚举值 = Rank(15);//甲子、己卯、甲午、己酉四上元之符头
            int delta = (int)this.Xml.枚举值 - (int)this.Xml.上元符头de枚举值;

            switch (delta / 5)
            {
                case 0:
                    this.Xml.所属之元de枚举值 = Persistence.Obj.enum元.上元;
                    break;
                case 1:
                    this.Xml.所属之元de枚举值 = Persistence.Obj.enum元.中元;
                    break;
                case 2:
                    this.Xml.所属之元de枚举值 = Persistence.Obj.enum元.下元;
                    break;
            }
        }


        private Persistence.Obj.enum甲子 Rank(int iDividby)
        {
            Persistence.Obj.enum甲子 retJZ;

            int iRank;
            int id = (int)this.Xml.枚举值;

            if (id % iDividby == 1) // this is the Rank
            {
                retJZ = this.Xml.枚举值;
            }
            else
            {
                if (id == 0) //癸亥 is 60ID
                {
                    iRank = (60 - 1) / iDividby;

                }
                else
                {
                    iRank = (id - 1) / iDividby;
                }


                retJZ = ZYOShared.ID2Enum<Persistence.Obj.enum甲子>(iRank * iDividby + 1);

            }

            return retJZ;
        }
    }

    public class Runtime节气 : ZYObject
    {
        public Persistence.Obj.节气 Xml;
        public Runtime节气()
            : base()
        {
            this.Xml = new ZYO.Persistence.Obj.节气();
            this.EnumType = typeof(Persistence.Obj.enum节气);
            this.PersistenceType = typeof(Persistence.Obj.节气);
        }

        public Runtime节气(int id, int year)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.ID2Enum<ZYO.Persistence.Obj.enum节气>(id);
            AssignProperties(year);

        }
        public Runtime节气(String strName, int year)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.Str2Enum<ZYO.Persistence.Obj.enum节气>(strName);
            AssignProperties(year);
        }


        public Runtime节气(DateTime dt)
            : this()
        {
            int Year = dt.Year;
            int Month = dt.Month;

            this.Xml.枚举值 = GetCurrentMonthJQID(Month);

            AssignProperties(Year);
        }

        public Runtime节气(Persistence.Obj.enum节气 enumjq, int year)
            : this()
        {
            this.Xml.枚举值 = enumjq;
            AssignProperties(year);

        }

        public Runtime节气(Persistence.Obj.节气 jq, int year)
            : this()
        {
            this.Xml = jq;
            AssignProperties(year);
        }

        private void AssignProperties(int year)
        {
            this.ID = (byte)this.Xml.枚举值;

            if (this.Xml.上元之局 == 0) //XML is not initialized yet
            {
                this.Xml.JQBaseTime1996 = DataFactory.ZYDBXML.节气Table[ID].JQBaseTime1996;

                this.Xml.上元之局 = DataFactory.ZYDBXML.节气Table[ID].上元之局;
                this.Xml.中元之局 = DataFactory.ZYDBXML.节气Table[ID].中元之局;
                this.Xml.下元之局 = DataFactory.ZYDBXML.节气Table[ID].下元之局;

                this.Xml.iLocation = DataFactory.ZYDBXML.节气Table[ID].iLocation;

                this.Xml.节气日期 = GetCurentMonthJQ(year);

                //计算 日 甲子===============================================================

                DateTime DT1997 = new DateTime(1997, 3, 22, 0, 0, 0, 0);// 1997,3,23是甲子日
                System.TimeSpan Duration = this.Xml.节气日期.Date - DT1997;

                int Dur = Duration.Days;

                while (Dur < 0)
                    Dur = Dur + 60000;

                this.Xml.日甲子de枚举值 = ZYOShared.ID2Enum<ZYO.Persistence.Obj.enum甲子>(Dur);

                // 上元符头 =======
                this.Xml.is闰 = false;

                Runtime甲子 day甲子 = new Runtime甲子(this.Xml.日甲子de枚举值);
                int delta = (int)this.Xml.日甲子de枚举值 - (int)day甲子.Xml.上元符头de枚举值; //delta should less than 15

                if (delta <= 10)
                {
                    this.Xml.节气上元符头de枚举值 = day甲子.Xml.上元符头de枚举值;
                    if (this.Xml.枚举值 == ZYO.Persistence.Obj.enum节气.大雪
                        || this.Xml.枚举值 == ZYO.Persistence.Obj.enum节气.芒种)  //可能置闰
                    {
                        if (delta == 10 || delta == 9) //置闰
                        {
                            this.Xml.is闰 = true;
                            this.Xml.闰节气上元符头de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum甲子>(
                                (int)day甲子.Xml.上元符头de枚举值 + 15); // 下一个符头
                        }
                    }
                    else
                    {
                        // bug: 刚刚置闰后的冬至、夏至，会有delta == 9，10，应该取下一个符头
                        // "大雪" 22 "芒种" 10
                        Runtime节气 jq大雪 = new Runtime节气("大雪", this.Xml.节气日期.Year);
                        Runtime节气 jq去年大雪 = new Runtime节气("大雪", this.Xml.节气日期.Year - 1);
                        Runtime节气 jq芒种 = new Runtime节气("芒种", this.Xml.节气日期.Year);

                        if ((((int)this.Xml.枚举值 - (int)jq大雪.Xml.枚举值) < 5) && jq大雪.Xml.is闰)
                        {
                            this.Xml.节气上元符头de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum甲子>((int)day甲子.Xml.上元符头de枚举值 + 15); // 下一个符头
                        }

                        if ((((int)this.Xml.枚举值 - (int)jq去年大雪.Xml.枚举值) < 5) && jq去年大雪.Xml.is闰)
                        {
                            this.Xml.节气上元符头de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum甲子>((int)day甲子.Xml.上元符头de枚举值 + 15); // 下一个符头
                        }

                        if ((((int)this.Xml.枚举值 - (int)jq芒种.Xml.枚举值) < 5) && jq芒种.Xml.is闰)
                        {
                            this.Xml.节气上元符头de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum甲子>((int)day甲子.Xml.上元符头de枚举值 + 15); // 下一个符头
                        }
                    }
                }
                else // next 符头
                {
                    this.Xml.节气上元符头de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum甲子>((int)day甲子.Xml.上元符头de枚举值 + 15); // 下一个符头
                }
            }// this.Xml.枚举值 != null

        }

        private Persistence.Obj.enum节气 GetCurrentMonthJQID(int Month)
        {
            if (Month < 0 || Month > 12)
            {
                throw new Exception(String.Format("Month - {0} is out of bound", Month.ToString()));
            }

            int ret;

            ret = 2 * (Month - 1) % 24;

            return ZYOShared.ID2Enum<Persistence.Obj.enum节气>(ret);
        }

        private DateTime GetCurentMonthJQ(int Year)
        {
            return (this.Xml.JQBaseTime1996.AddDays((Year - 1996) * Double.Parse(Resources.DefaultValues.太阳年常数)));
        }

        //public static class节气 operator +(class节气 jq, int delta)
        //{
        //    int newJQID = jq.ID + delta;
        //    int newYear = jq.节气日期.Year;

        //    if (newJQID >= jq.Sum)
        //    {
        //        newYear += newJQID / jq.Sum;
        //    }
        //    if (newJQID < 0)
        //    {
        //        newYear += newJQID / jq.Sum - 1;
        //    }

        //    return new class节气(newJQID, newYear);
        //}

        //public static class节气 operator ++(class节气 jq)
        //{
        //    int newJQID = jq.ID + 1;
        //    int newYear = jq.节气日期.Year;

        //    if (newJQID == jq.Sum) //jq == 冬至 ID == 23
        //    {
        //        newJQID = 0;
        //        newYear++;
        //    }
        //    return new class节气(newJQID, newYear);
        //}

        //public static class节气 operator --(class节气 jq)
        //{
        //    int newJQID = jq.ID - 1;
        //    int newYear = jq.节气日期.Year;

        //    if (newJQID == -1) //jq == 小寒 ID == 0
        //    {
        //        newJQID = 23;
        //        newYear--;
        //    }
        //    return new class节气(newJQID, newYear);
        //}

    }

    public class Runtime八卦 : ZYObject
    {
        public Persistence.Obj.八卦 Xml;

        public Runtime八卦()
            : base()
        {
            this.Xml = new ZYO.Persistence.Obj.八卦();
            this.EnumType = typeof(Persistence.Obj.enum八卦ID);
            this.PersistenceType = typeof(Persistence.Obj.八卦);
        }

        public Runtime八卦(int id)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum八卦ID>(id);
            AssignProperties();
        }
        public Runtime八卦(String strName)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.Str2Enum<ZYO.Persistence.Obj.enum八卦ID>(strName);
            AssignProperties();
        }


        public Runtime八卦(Persistence.Obj.enum八卦ID eTG)
            : this()
        {
            this.Xml.枚举值 = eTG;
            AssignProperties();
        }

        public Runtime八卦(Persistence.Obj.八卦 persistTianGan)
            : this()
        {
            this.Xml = persistTianGan;
            this.ID = (byte)this.Xml.枚举值;

        }

        public override void AssignProperties()
        {
            this.ID = (byte)this.Xml.枚举值;

            this.Xml.五行属性de枚举值 = (Persistence.Obj.enum五行)DataFactory.ZYDBXML.八卦Table[this.ID].五行属性de枚举值;

            this.Xml.先天数de枚举值 = (Persistence.Obj.enum先天八卦)DataFactory.ZYDBXML.八卦Table[this.ID].先天数de枚举值;
            this.Xml.方向 = DataFactory.ZYDBXML.八卦Table[this.ID].方向;

            this.Xml.下卦地支de枚举值 = (Persistence.Obj.enum地支)DataFactory.ZYDBXML.八卦Table[this.ID].下卦地支de枚举值;
            //this.Xml.上卦地支de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum地支>((int)queryRow[4]);

            this.Xml.阴阳 = DataFactory.ZYDBXML.八卦Table[this.ID].阴阳;
        }
    }

    public class Runtime六十四卦 : ZYObject
    {
        public Persistence.Obj.六十四卦 Xml;

        public Runtime六十四卦()
            : base()
        {
            this.Xml = new ZYO.Persistence.Obj.六十四卦();
            this.EnumType = typeof(Persistence.Obj.enum六十四卦ID);
            this.PersistenceType = typeof(Persistence.Obj.六十四卦);
        }

        public Runtime六十四卦(String strName, byte 变卦记录ID)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.Str2Enum<Persistence.Obj.enum六十四卦ID>(strName);
            this.Xml.变卦记录ID = 变卦记录ID;
            AssignProperties();
        }
        public Runtime六十四卦(int id, byte 变卦记录ID)
            : this()
        {
            this.Xml.枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum六十四卦ID>(id);
            this.Xml.变卦记录ID = 变卦记录ID;
            AssignProperties();
        }

        public Runtime六十四卦(Persistence.Obj.enum六十四卦ID 卦枚举值, byte 变卦记录ID)
            : this()
        {
            this.Xml.枚举值 = 卦枚举值;
            this.Xml.变卦记录ID = 变卦记录ID;
            AssignProperties();
        }

        public Runtime六十四卦(Persistence.Obj.六十四卦 卦XML)
            : this()
        {
            this.Xml = 卦XML;
            this.ID = (byte)this.Xml.枚举值;
        }

        public override void AssignProperties()
        {
            this.ID = (byte)this.Xml.枚举值;
            this.Xml.变卦de枚举值 = Get变卦枚举值(this.Xml.变卦记录ID);

            int iShiID = DataFactory.ZYDBXML.六十四卦Table[this.ID].世爻;
            int iYingID = mod((iShiID + 3), 6);

            int iUP = (int)this.Xml.枚举值 / 8;
            int iDown = (int)this.Xml.枚举值 % 8;
            int iBelong = DataFactory.ZYDBXML.六十四卦Table[this.ID].八卦卦宫;

            this.Xml.序号Number = (Persistence.Obj.enum六十四卦by数)DataFactory.ZYDBXML.六十四卦Table[this.ID].周易序号;//乾为天=1,有效值1到64
            this.Xml.卦象 = DataFactory.ZYDBXML.六十四卦Table[this.ID].卦象.Trim(); //卦象Meaning
            this.Xml.彖曰 = DataFactory.ZYDBXML.六十四卦Table[this.ID].彖曰.Trim(); //彖曰 zhuan
            this.Xml.象曰 = DataFactory.ZYDBXML.六十四卦Table[this.ID].象曰.Trim();//象曰 xiang

            this.Xml.上卦 = new Runtime八卦(iUP).Xml;
            this.Xml.下卦 = new Runtime八卦(iDown).Xml;
            this.Xml.八卦卦宫 = new Runtime八卦(iBelong).Xml;

            this.Xml.五行属性de枚举值 = this.Xml.八卦卦宫.五行属性de枚举值;
            this.Xml.互卦de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum六十四卦ID>((((byte)this.Xml.枚举值 << 1) & 56) | (((byte)this.Xml.枚举值 >> 1) & 7));

            //if ((int)this.Xml.枚举值 == (int)this.Xml.八卦卦宫.枚举值 * 9) // 本卦就是卦宫
            //卦宫8.ID 为下卦ID; 卦宫8.ID*8 为上卦ID 
            this.Xml.卦宫de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum六十四卦ID>((byte)this.Xml.八卦卦宫.枚举值 * 9);

            this.Xml.爻s = new Persistence.Obj.爻[6];


            // public void 初始化爻s(Persistence.Obj.enum五行 本卦的五行)
            for (int i = 0; i < 6; i++)
            {
                this.Xml.爻s[i] = Ini爻(i, this.Xml, this.Xml.五行属性de枚举值);
            }

            this.Xml.世爻 = this.Xml.爻s[iShiID - 1];
            this.Xml.应爻 = this.Xml.爻s[iYingID - 1];

            if (this.Xml.变卦记录ID != 0)
            {
                //提取变爻
                Persistence.Obj.六十四卦 ChangedGua = new Runtime六十四卦(this.Xml.变卦de枚举值, 0).Xml;
                for (int i = 0; i < this.Xml.爻s.Length; i++)
                {
                    this.Xml.爻s[i].动否 = (((byte)this.Xml.枚举值 ^ (byte)this.Xml.变卦de枚举值) & (int)System.Math.Pow(2, i)) != 0; //Binary AND
                    if (this.Xml.爻s[i].动否)
                    {
                        this.Xml.爻s[i].爻辞 = DataFactory.ZYDBXML.易经爻辞Table[this.ID].爻辞[i].Trim();
                        this.Xml.爻s[i].变爻 = Ini爻(i, ChangedGua, this.Xml.五行属性de枚举值);
                    }
                }
            }
        }

        public Persistence.Obj.enum六十四卦ID Get变卦枚举值(int 变卦记录ID)
        {
            return ZYOShared.ID2Enum<Persistence.Obj.enum六十四卦ID>((byte)this.Xml.枚举值 ^ 变卦记录ID); // Binary OR
        }

        public Persistence.Obj.爻 Ini爻(int 序号0起, Persistence.Obj.六十四卦 本卦, Persistence.Obj.enum五行 本卦的五行)
        {
            Persistence.Obj.爻 ret爻 = new ZYO.Persistence.Obj.爻();

            ret爻.序号0起 = (byte)序号0起;

            ret爻.阴阳 = (((int)本卦.枚举值 >> ret爻.序号0起) % 2) == 1;

            Persistence.Obj.enum地支 dz本卦初始地支;
            if (序号0起 < 3)	//下卦
            {
                dz本卦初始地支 = 本卦.下卦.下卦地支de枚举值;
            }
            else //上卦
            {
                dz本卦初始地支 = 本卦.上卦.下卦地支de枚举值;
            }

            //下挂上卦的公式相同都用pDownDZ计算
            if (new Runtime地支(dz本卦初始地支).Xml.阴阳 == true)
            {
                //阳性地支
                ret爻.地支de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum地支>((int)dz本卦初始地支 + ret爻.序号0起 * 2);
            }
            else
            {
                //阴性地支
                ret爻.地支de枚举值 = ZYOShared.ID2Enum<Persistence.Obj.enum地支>((int)dz本卦初始地支 - ret爻.序号0起 * 2);
            }

            ret爻.五行属性de枚举值 = new Runtime地支(ret爻.地支de枚举值).Xml.五行属性de枚举值;
            // Mapping 生克关系 = ZYOEnum.六神
            ret爻.六亲de枚举值 = (Persistence.Obj.enum六神)ZYOShared.Check五行生克(ret爻.五行属性de枚举值, 本卦的五行);

            ret爻.Name = ret爻.六亲de枚举值.ToString() + ret爻.地支de枚举值.ToString() + ret爻.五行属性de枚举值.ToString();

            return ret爻;

        }

    }
}

//最大公约数和最小公倍数
//void main()
//{
//    int m(int x,int y);
//    int a,b,t,p,q;
//    printf("请输入两个正整数:\n");
//    scanf("%d,%d",&p,&q);
//    if (p==q)
//    {
//        printf("最大公约数和最小公倍数均为:%d\n",p);
//    }
//    else if(p<q)
//    {
//        t=p;
//        p=q;
//        q=t;
//        a=m(p,q);      //调用一个函数
//        b=p*q/a    ;  //两个数的乘积等于它们最大公约数和最小公倍数的乘积
//        printf("最大公约数为:%d\n",a);
//        printf("最小公倍数为:%d\n",b);
//    }
//    else
//    {
//        a=m(p,q);     //同上
//        b=p*q/a;
//        printf("最大公约数为:%d\n",a);
//        printf("最小公倍数为:%d\n",b);
//    }
//}
////用辗转相除法求最大公约数
//# include<math.h>
// int m(int x,int y)
//{
//    int z;
//    while(y!=0)
//    {
//        z=x%y;
//        x=y;
//        y=z;
//    }
//    return(x)   ;
//}

//最大公约数 采用辗转相除法可以获得，
//最小公倍数 就是 x*y/最大公约数  了 ....


//用辗转相除法求两个数的最大公约数的步骤如下：
//先用小的一个数除大的一个数，得第一个余数；
//再用第一个余数除小的一个数，得第二个余数；
//又用第二个余数除第一个余数，得第三个余数；
//这样逐次用后一个数去除前一个余数，直到余数是0为止。那么，最后一个除数就是所求的最大公约数（如果最后的除数是1，那么原来的两个数是互质数）。

//例如求1515和600的最大公约数，

//第一次：用600除1515，商2余315；
//第二次：用315除600，商1余285；
//第三次：用285除315，商1余30；
//第四次：用30除285，商9余15；
//第五次：用15除30，商2余0。

//1515和600的最大公约数是15。

//辗转相除法是求两个数的最大公约数的方法。如果求几个数的最大公约数，可以先求两个数的最大公约数，再求这个最大公约数与第三个数的最大公约数。这样依次下去，直到最后一个数为止。最后所得的一个最大公约数，就是所求的几个数的最大公约数。
